package com.fjec.activity.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.activity.feign.BaseClient;
import com.fjec.activity.feign.MarketClient;
import com.fjec.common.jxls.ExchangeGiftExcel;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.setting.Setting;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.JsonUtil;
import org.springframework.web.bind.annotation.*;

import com.fjec.activity.service.IBizExchangeGiftService;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 010.兑换礼物表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/")
public class BizExchangeGiftController {

    @Resource
    private IBizExchangeGiftService service;

    @Resource
    private MarketClient marketClient;

    @Resource
    private BaseClient baseClient;

    /**
     * 用户兑换礼物数据保存
     *
     * @param ids
     * @param giftIds
     * @param nums
     * @return
     */
    @PostMapping("/y/user/gift/exchange")
    public JsonResult userExchangeGift(@RequestParam String ids,
                                       @RequestParam String giftIds,
                                       @RequestParam String nums) throws Exception {
        return service.userExchangeGift(ids, giftIds, nums);
    }

    /**
     * 用户兑换礼物数据列表
     *
     * @return
     */
    @GetMapping("/y/user/gift/exchange/list")
    public JsonResult getExchangeGiftList() throws Exception {
        return service.getExchangeGiftList();
    }

    /**
     * 用户兑换礼物数据列表
     *
     * @param page
     * @param searchBy  搜索类型 1=店铺名 2=行为编号
     * @param condition 店铺名
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @throws Exception
     */
    @GetMapping("/user/gift/exchange/page")
    public Map<String, Object> getExchangeGiftPage(Page page,
                                                   @RequestParam Integer searchBy,
                                                   @RequestParam(required = false) String condition,
                                                   @RequestParam(required = false) String startTime,
                                                   @RequestParam(required = false) String endTime) throws Exception {
        LocalDate sTime = StringUtils.isEmpty(startTime) ? null : DateUtil.parseLd(startTime, "yyyy-MM-dd");
        LocalDate eTime = StringUtils.isEmpty(endTime) ? null : DateUtil.parseLd(endTime, "yyyy-MM-dd");
        return service.getExchangeGiftPage(page, searchBy, condition, sTime, eTime);
    }

    /**
     * 根据时间获取当天的用户兑换申请
     *
     * @param date
     * @return
     */
    @GetMapping("/exchange/gift/for/excel")
    public List<ExchangeGiftExcel> getExchangeGiftForExcel(@RequestParam String date) {
        return service.getExchangeGiftForExcel(DateUtil.parseLd(date, "yyyyMMdd"));
    }

    @GetMapping("/activity/test")
    public String test(String ids) {
//        Map<String, ServiceProjectVO> list = marketClient.getServiceProjectByIds(ids);
        Map<String, Object> settingMap = baseClient.getSysSetting();
        Setting setting = new Setting(settingMap);
        return JsonUtil.toJson(setting);
    }
}
